package com.llsydn.service.impl;

import com.llsydn.entity.ShopInfo;
import com.llsydn.service.AndroidService;
import com.llsydn.utils.CommonUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 请填写相关描述
 * @date 2018/9/28 8:50
 */
@Service
public class AndroidServiceImpl implements AndroidService {
    Pattern pattern = Pattern.compile("[^0-9]");

    /**
     * 得到所有商品信息的集合
     *
     * @param request
     * @return
     */
    @Override
    public List<ShopInfo> getAllShops(HttpServletRequest request) {
        List<ShopInfo> list = new ArrayList<ShopInfo>();
        // 得到images文件夹的真实路径
        String projectPath = CommonUtils.getProjectPath();
        try {
            if (projectPath.indexOf(".jar") > -1) {
                JarFile jarFile = new JarFile(new File(projectPath));
                Enumeration<JarEntry> entries = jarFile.entries();
                int index = 0;
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    //得到商品的相关信息
                    if (!entry.isDirectory() && entry.getName().indexOf("/static/images/") > -1 && entry.getName().endsWith(".jpg")) {
                        String idName = entry.getName();
                        String imageName = entry.getName();
                        Matcher matcher = pattern.matcher(idName);
                        //商品id
                        int id = Integer.parseInt(matcher.replaceAll(""));
                        imageName = imageName.substring(imageName.indexOf("/static/images/") + 15, imageName.length());
                        //商品名称
                        String name = "f" + id + "的商品名称";
                        //图片路径
                        String imagePath = "http://" + request.getLocalAddr() + ":" + request.getLocalPort() + request.getContextPath() + "/images/" + imageName;
                        //图片价格
                        float price = new Random().nextInt(20) + 20;
                        //商品id
                        //封装成对象
                        ShopInfo info = new ShopInfo(id, name, price, imagePath);
                        //添加到集合中
                        list.add(info);
                        index++;
                    }
                }
            } else {
                //创建images文件夹File对象
                File file = new File(projectPath + "/static/images");
                //得到images文件夹下所有图片文件的file对象数组
                File[] files = file.listFiles();
                //遍历
                for (int i = 0; i < files.length; i++) {
                    //得到商品的相关信息
                    int id = i + 1;
                    //图片名称
                    String imageName = files[i].getName();
                    //商品名称
                    String name = imageName.substring(0, imageName.lastIndexOf(".")) + "的商品名称";
                    //图片路径
                    String imagePath = "http://" + request.getLocalAddr() + ":" + request.getLocalPort() + request.getContextPath() + "/images/" + imageName;
                    //图片价格
                    float price = new Random().nextInt(20) + 20;
                    //封装成对象
                    ShopInfo info = new ShopInfo(id, name, price, imagePath);
                    //添加到集合中
                    list.add(info);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对list集合排序
        Collections.sort(list, new Comparator<ShopInfo>() {
            @Override
            public int compare(ShopInfo o1, ShopInfo o2) {
                int i = o1.getId() - o2.getId();
                return i;
            }
        });
        return list;
    }
}
